import TinyLayer from "./tinylayer.vue";
import { createApp } from "vue-demi";
import util from "./util.js";

TinyLayer.install = Vue => {
    Vue.component( "TinyLayer", TinyLayer );

    let instanceLoading = null;
    let instanceConfirm = null;
    let instancePrompt = null;
    let instanceAlert = null;

    if ( typeof Vue === "function" ) {
        const TinyLayerConstructor = Vue.extend( TinyLayer );
        instanceLoading = new TinyLayerConstructor();
        instanceConfirm = new TinyLayerConstructor();
        instancePrompt = new TinyLayerConstructor();
        instanceAlert = new TinyLayerConstructor();
        [ 
            instanceLoading, 
            instanceConfirm, 
            instancePrompt, 
            instanceAlert 
        ].forEach(instance => {
            instance.$mount( util.createDiv() );
            util.appendToBody( instance.$el );
        })
    }

    if ( typeof Vue === "object" ) {
        instanceLoading = createApp( TinyLayer ).mount( util.createDiv() );
        instanceConfirm = createApp( TinyLayer ).mount( util.createDiv() );
        instancePrompt = createApp( TinyLayer ).mount( util.createDiv() );
        instanceAlert = createApp( TinyLayer ).mount( util.createDiv() );
        [ 
            instanceLoading, 
            instanceConfirm, 
            instancePrompt, 
            instanceAlert 
        ].forEach(instance => {
            util.appendToBody( instance.$el );
        })
    }

    function alertMode ( type, ...arg ) {
        const [ a1, a2 ] = arg;
        if ( !a1 ) {
            return;
        }
        let obj = {};
        if ( util.isPlainObject( a1 ) ) {
            obj = a1;
        } else {
            obj.title = a1;
            obj.content = a2;
        }
        instanceAlert.alert.type = type;
        util.lacpPublicOpen( instanceAlert, "alert", obj );
    }

    const opt = {
        loading: {
            open ( ...arg ) {
                let obj = {};
                if ( util.isPlainObject( arg[ 0 ] ) ) {
                    obj = arg[ 0 ];
                } else {
                    obj.text = arg[ 0 ];
                }
                util.lacpPublicOpen( instanceLoading, "loading", obj );
            },
            close: () => util.lacpPublicClose( instanceLoading )
        },
        alert: {
            info: ( ...arg ) => alertMode( "info", ...arg ),
            success: ( ...arg ) => alertMode( "success", ...arg ),
            warning: ( ...arg ) => alertMode( "warning", ...arg ),
            error: ( ...arg ) => alertMode( "error", ...arg )
        },
        confirm: {
            open: options => util.lacpPublicOpen( instanceConfirm, "confirm", options ),
            close: () => util.lacpPublicClose( instanceConfirm )
        },
        prompt: {
            open: options => util.lacpPublicOpen( instancePrompt, "prompt", options ),
            close: () => util.lacpPublicClose( instancePrompt )
        }
    }

    if ( typeof Vue === "function" ) {
        Vue.prototype.$tinylayer = opt;
    }
    if ( typeof Vue === "object" ) {
        Vue.config.globalProperties.$tinylayer = opt;
    }
} 

export default TinyLayer;